public class run implements Runnable{

    private static int i=100;
    @Override
    public void run() {

        while (i>0) {
            //无线程安全
            //        if (i > 0) {
//            i--;
//            System.out.println(Thread.currentThread().getName() + "==========" + i);
//        }
            //保证线程安全
            synchronized (Object.class) {
                if (i > 0) {
                    i--;
                    System.out.println(Thread.currentThread().getName() + "==========" + i);
                }
            }
            try {
                Thread.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    static class demo{
        public static void main(String[] args) {
            Thread a = new Thread(new run(), "A");
            Thread b = new Thread(new run(), "B");
            run run = new run();

            a.start();
            b.start();


        }
    }
}
